package DAO;

import Logica.Address;
import Logica.Telephone;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.Vector;

public class OwnerDao {
  public OwnerDao() {
  }

  public boolean creaOwn(String FirstName, String LastName,
                         int ClinicNumber) {
    float numberO=-5;
    Conexion conexion = new Conexion();
    boolean creado = false;
    Connection conex;

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;
        ResultSet rsa = null;
        int mayor = -1;
        ps = conex.prepareStatement("select max (OwnerNumber)  from owner");
        
        rsa = ps.executeQuery();
        if (rsa != null) {
          while (rsa.next()) {
            mayor = rsa.getInt(1);
          }
        } else
          numberO = 1;
    numberO=mayor+1;
 
      ps =
          conex.prepareStatement("INSERT INTO OWNER (CLINICNUMBER, FIRSTNAME, LASTNAME, OWNERNUMBER) VALUES (?,?,?,?)");

      ps.setInt(1, ClinicNumber);
      ps.setString(2, FirstName);
      ps.setString(3, LastName);
      ps.setFloat(4, numberO);
          

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);
      creado = true;
      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());

    }
    return creado;
  }
  
    public boolean eliminarOwner(int numberO) {

      Conexion conexion = new Conexion();
      boolean eliminado = false;
      Connection conex;

      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        ps = conex.prepareStatement("DELETE FROM Owner where OwnerNumber = ?");

        ps.setInt(1, numberO);


        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que borro fueron" +
                           numeroFilas);
        eliminado = true;
        conex.close();

      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());

      }
      return eliminado;
    }
    
    public boolean modificaOwnClinicNumber(int numberO,  int cn)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Owner set clinicNumber = ?  where OwnerNumber = ?"); 
        
        ps.setInt(1, cn);
        ps.setInt(2, numberO);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
    public boolean modificaOwnFirstName(int numberO,  String fname)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Owner set FirstName = ?  where OwnerNumber = ?"); 
        
        ps.setString(1, fname);
        ps.setInt(2, numberO);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
    public boolean modificaOwnLastName(int numberO,  String lname)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Owner set LastName = ?  where OwnerNumber = ?"); 
        
        ps.setString(1, lname);
        ps.setInt(2, numberO);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
  public static Vector <Vector<String> > Consultar_Owner() {
         Vector <Vector<String> > matriz= new Vector <Vector<String> >();
       Vector<String> fila=new  Vector<String>();
         Vector<String> id=new  Vector<String>();
       Conexion conexion = new Conexion();
       Connection conex;

       try {
         conex = conexion.getConnection();
         PreparedStatement ps = null;
         ResultSet rs = null;

         ResultSet rsa = null;
         String nombre =new String("");
         ps = conex.prepareStatement("select ownerNumber, firstName, lastName from owner");
         rsa = ps.executeQuery();
                             
         if (rsa != null) {
           while (rsa.next()) {
            fila.add(rsa.getString("ownerNumber"));
                  id.add(rsa.getString("ownerNumber"));
              }
         }           
         conex.close();

       } catch (SQLException ex) {
         System.err.println("SQLException: " + ex.getMessage());

       }
         matriz.add(id);
         matriz.add(fila);
       return matriz;
     }
}
